package br.uece.tcc.fh.jxta.util.message;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Enumeration;

import net.jxta.document.Document;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.TextElement;

public class FhJxtaResponseMessage {

	/**
     * The base from the original query.
     */
    private String sqlQuery = null;

    /**
     * The power from the original query.
     */
    private String sqlResultSet = null;

   
    /**
     * Creates a response object using the given answer value.
     *
     * @param   anAnswer the answer for the response.
     */
    public FhJxtaResponseMessage(String aSqlQuery, String aSqlResultSet)
    {
        this.sqlQuery = aSqlQuery;
        this.sqlResultSet = aSqlResultSet;
    }

    /**
     * Creates a response object by parsing the given input stream.
     *
     * @param       stream the InputStream source of the response data.
     * @exception   IOException if an error occurs reading the stream.
     */
    public FhJxtaResponseMessage(InputStream stream) throws IOException
    {
        StructuredTextDocument document = (StructuredTextDocument)
            StructuredDocumentFactory.newStructuredDocument(
                new MimeMediaType("text/xml"), stream);

        Enumeration elements = document.getChildren();

        while (elements.hasMoreElements())
        {
            TextElement element = (TextElement) elements.nextElement();
            
            if(element.getName().equals("sqlQuery"))
            {
                sqlQuery = element.getTextValue();
                continue;
            }
            
            if(element.getName().equals("sqlResultSet"))
            {
                sqlResultSet = element.getTextValue();
                continue;
            }
            
        }
    }

    /**
     * Returns the answer for the response.
     *
     * @return  the answer value for the response.
     */
    public String getSqlQuery()
    {
        return sqlQuery;
    }

    /**
     * Returns the base for the query.
     *
     * @return  the base value for the query.
     */
    public String getSqlResultSet()
    {
        return sqlResultSet;
    }

    /**
     * Returns a Document representation of the response.
     *
     * @param   asMimeType the desired MIME type representation for 
     *          the response.
     * @return  a Document form of the response in the specified MIME
     *     representation.
     */
    public Document getDocument(MimeMediaType asMimeType)
    {
        Element element;
        StructuredDocument document = (StructuredTextDocument)
            StructuredDocumentFactory.newStructuredDocument(
                asMimeType, "example:ExampleResponse");

        element = document.createElement(
            "sqlQuery", getSqlQuery());
        document.appendChild(element);
        
        element = document.createElement("sqlResultSet", 
            getSqlResultSet());
        document.appendChild(element);

        return document;
    }

   
    /**
     * Returns an XML String representation of the response.
     *
     * @return  the XML String representing this response.
     */
    public String toString()
    {
        try
        {
            StringWriter buffer = new StringWriter();
            StructuredTextDocument document = (StructuredTextDocument)
                getDocument(new MimeMediaType("text/xml"));
            document.sendToWriter(buffer);

            return buffer.toString();
        }
        catch (Exception e)
        {
            return "";
        }
    }

}
